package com.symaster.gameengine.moba;

import com.symaster.common.animation.Animation;
import com.symaster.common.animation.Curve;

public class SpeedManager extends MoveManager {

    public float maxSpeed = 0f;
    private float speedScale = 0f;
    private final Animation animation;
    private boolean start = false;
    private final Update update;

    public SpeedManager(Update update) {
        this.update = update;
        this.animation = getAnimation();
    }

    protected Animation getAnimation() {
        return new Animation();
    }

    @Override
    void start(float angle) {
        if (start) {
            return;
        }
        start = true;

        Animation.AnimationListener animationListener = new Animation.AnimationListener(Curve.TYPE_BALANCED, Animation.AnimationConfig.ofTime(300, speedScale, 1)) {
            @Override
            public void handler(double factor) {
                SpeedManager.this.speedScale = (float) factor;
                update();
            }
        };

        animation.execution(Animation.Sequence.SEQUENCE_TURN, Animation.Strategy.StopAtCurrentPosition, animationListener);
    }

    @Override
    void stop() {
        if (!start) {
            return;
        }
        start = false;
        Animation.AnimationListener animationListener = new Animation.AnimationListener(Curve.TYPE_BALANCED, Animation.AnimationConfig.ofTime(100, speedScale, 0)) {
            @Override
            public void handler(double factor) {
                SpeedManager.this.speedScale = (float) factor;
                update();
            }
        };
        animation.execution(Animation.Sequence.SEQUENCE_TURN, Animation.Strategy.StopAtCurrentPosition, animationListener);
    }

    void update() {
        update.update(speedScale * maxSpeed);
    }
}